using System;
using System.Collections.Generic;
using System.Xml;
using System.Windows.Forms;

namespace WPRotator
{
    /// <summary>
    /// Save the setting in xml file
    /// </summary>
    public class Settings
    {
        private string settingsPath;
        private int interval;
        private PictureList pictureList;
        private bool random;

        public bool Random
        {
            get { return random; }
            set { random = value; }
        }

        public string SettingsPath
        {
            get { return settingsPath; }
            set { settingsPath = value; }
        }

        public int Interval
        {
            get { return interval; }
            
            set 
            {
                interval = value;
            }
        }

        public PictureList PictureList
        {
            get { return pictureList; }
        }
           

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="localpath"></param>
        public Settings(string localpath, CheckedListBox list)
        {
            settingsPath = localpath;
            pictureList = new PictureList(list);
        }

        #region save and load default values
        /// <summary>
        /// save the setting to xml file
        /// </summary>
        public void SaveSettings()
        {
            XmlTextWriter writer = new XmlTextWriter(settingsPath + "\\settings.xml", null);
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 4;

            writer.WriteComment("Settings for WallPaper changer program");

            writer.WriteStartElement("settings");

            writer.WriteElementString("picIndex", PictureList.SelectedIndex.ToString());
            writer.WriteElementString("interval", Interval.ToString());
            writer.WriteElementString("random", random.ToString());

            writer.WriteStartElement("pictures");

            foreach (var item in pictureList.FilenameList)
            {
                writer.WriteStartElement("file");
                writer.WriteAttributeString("path", item);
                writer.WriteEndElement();
            }

            writer.WriteEndElement();
            writer.WriteEndElement();

            writer.Flush();
            writer.Close();
        }

        /// <summary>
        /// load the setting from xml file to the application
        /// </summary>
        public void LoadSettings()
        {
            XmlTextReader reader = null;
            string elementName = null;
            try
            {
                reader = new XmlTextReader(settingsPath + "\\settings.xml");
                reader.WhitespaceHandling = WhitespaceHandling.None;

                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        elementName = reader.Name;

                        if (elementName == "file")
                        {
                            PictureList.Add(reader["path"]);
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.Text)
                    {
                        switch (elementName)
                        {
                            case "picIndex":
                                PictureList.SelectedIndex = Int32.Parse(reader.Value);
                                break;
                            case "interval":
                                Interval = Int32.Parse(reader.Value);
                                break;
                            case "random":
                                random = Boolean.Parse(reader.Value);
                                break;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            if (reader != null)
                reader.Close();
        }
        #endregion
    }
}